/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.pages.game.find;

import java.io.IOException;
import java.util.Iterator;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import es.sonxurxo.androidrunner.model.persistence.util.exceptions.InstanceNotFoundException;
import es.sonxurxo.androidrunner.model.service.game.to.GameTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.components.game.list.GameListPanel;
import es.sonxurxo.androidrunner.view.web.pages.BasePage;
import es.sonxurxo.androidrunner.view.web.ws.util.ExceptionCodes;
import es.sonxurxo.androidrunner.view.xml.ServletUtils;
import es.sonxurxo.androidrunner.view.xml.TOToXMLConversor;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class GamesListPage extends BasePage {

	private static final int RESULTS_PER_PAGE = 5;
	
	public enum STRATEGY {
		ACTIVE, ALL
	}

	public GamesListPage(STRATEGY strategy) {
		this(null, strategy);
	}
	
	public GamesListPage(PageParameters pageParameters, STRATEGY strategy) {
		super(pageParameters);
		
		try {

			this.add(new GameListPanel("gamesPanel", new GameProvider(strategy), GamesListPage.RESULTS_PER_PAGE));
			
		} catch (Exception e) {
			try {
				ServletUtils.writeServiceResponse(TOToXMLConversor.toXML(e,
						ExceptionCodes.GENERAL_CODE), this.getResponse());
			} catch (IOException io) {
				e.printStackTrace();
				System.out.println("IOIO : " + e.getMessage());			
			}
		}
	}
	
	class GameProvider implements IDataProvider  {
		
		private static final long serialVersionUID = -7749054103035343106L;
		
		private Integer resultsCount;
		private STRATEGY strategy;
		
		public GameProvider(STRATEGY strategy) {
			super();
			this.strategy = strategy;
		}

		@SuppressWarnings("unchecked")
		public Iterator iterator(int startIndex, int count) {
			switch (this.strategy) {
			case ACTIVE:
				return AndroidRunnerApplication.get().getGameService()
				.findActiveGames(startIndex, count).iterator();
			case ALL:
				return AndroidRunnerApplication.get().getGameService()
				.findNotFinishedGames(startIndex, count).iterator();
			default:
				return AndroidRunnerApplication.get().getGameService()
				.findNotFinishedGames(startIndex, count).iterator();
			}
		}

		public int size() {
			switch (this.strategy) {
			case ACTIVE:
				if (this.resultsCount == null) {
					this.resultsCount = AndroidRunnerApplication.get().getGameService()
					.countActiveGames();
				}
				break;
			case ALL:
				if (this.resultsCount == null) {
					this.resultsCount = AndroidRunnerApplication.get().getGameService()
					.countNotFinishedGames();
				}
				break;
			default:
				if (this.resultsCount == null) {
					this.resultsCount = AndroidRunnerApplication.get().getGameService()
					.countNotFinishedGames();
				}
			}
			return this.resultsCount.intValue();
		}

		public IModel model(Object object) {
			return new GameModel((GameTO) object);
		}

		protected class GameModel extends LoadableDetachableModel {
			
			private static final long serialVersionUID = -5423144904529853027L;
			
			private Long id;

			public GameModel(GameTO gameTO) {
				this.id = new Long(gameTO.getGameId());
			}

			@Override
			protected Object load() {
				try {
					return AndroidRunnerApplication.get().getGameService().findGameById(this.id.longValue());
				} catch (InstanceNotFoundException e) {
					return null;
				}
			}
		}

		public void detach() {
			//Not needed            
		}
	}
	
	@Override
	protected String getTitle() {
		return getLocalizer().getString("gamesList.title", GamesListPage.this);
	}
}
